﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace System
{
    public static class NumberExtensions
    {
        /// <summary>
        /// string 转换 2次幂总和
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static int ToPowInt(this List<int> source)
        {
            int num = 0;
            foreach (var item in source)
            {
                num += (int)Math.Pow(2, item);
            }
            return num;
        }

        /// <summary>
        /// string 转换 2次幂总和
        /// </summary>
        /// <param name="source">string</param>
        /// <param name="defaultValue"></param>
        /// <returns>System.Int32.</returns>
        public static int ToPowInt(this int source)
        {
            return (int)Math.Pow(2, source);
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(this double FactSize)
        {
            string m_strSize = "";
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(this int FactSize)
        {
            string m_strSize = "";
            if (FactSize < 1024)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024 && FactSize < 1048576)
                m_strSize = (FactSize / 1024).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024 / 1024).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024 / 1024 / 1024).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(this long FactSize)
        {
            string m_strSize = "";
            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " Byte";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
            return m_strSize;
        }

        /// <summary>
        /// int 转化为 枚举对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="value">待转化的int</param>
        /// <returns>T.</returns>
        public static T ToEnum<T>(this int value) where T : struct
        {
            Type typeFromHandle = typeof(T);
            if (!typeFromHandle.IsEnum)
            {
                throw new ArgumentException(string.Format("类型{0}不是枚举类型。", typeFromHandle.Name));
            }

            if (!Enum.TryParse(value.ToString(), out T result))
            {
                throw new ArgumentException(string.Format("{0}不能转换成枚举类型{1}。", value, typeFromHandle.Name));
            }
            return result;
        }
    }
}
